package com.cvte.sunkit.manage

/**
 *  @PackageName com.cvte.sunkit.manage
 *  @Description  IO异常数据的监听类，更新异常计数器和异常状态和阈值
 *  @Author lixu
 *  @Data 2023/7/19 16:58
 *  @Version 1.0
 */
object IOMonitor {
    private var mInCount = 0
    private var mOutCount = 0
    private var isInAbnormal = false
    private var isOutAbnormal = false
    private var mThresholdIOIn = 0.1 * 1024
    private var mThresholdIOOut = 0.1 * 1024

    private var mInAbnormalCnt = 0
    private var mOutAbnormalCnt = 0


    fun updateCount(inSpeed: Double, outSpeed: Double) {
        if (inSpeed > mThresholdIOIn) {
            addInCount()
            if (mInCount == 3) {
                isInAbnormal = true
            }
        } else {
            subInCount()
            if (mInCount == 0) {
                isInAbnormal = false
            }
        }

        if (outSpeed > mThresholdIOOut) {
            addOutCount()
            if (mOutCount == 3) {
                isOutAbnormal = true
            }
        } else {
            subOutCount()
            if (mOutCount == 0) {
                isOutAbnormal = false
            }
        }
    }

    private fun addInCount() {
        mInCount = if (mInCount < 3) mInCount + 1 else 3
    }

    private fun subInCount() {
        mInCount = if (mInCount > 0) mInCount - 1 else 0
    }

    private fun addOutCount() {
        mOutCount = if (mOutCount < 3) mOutCount + 1 else 3
    }

    private fun subOutCount() {
        mOutCount = if (mOutCount > 0) mOutCount - 1 else 0
    }

    fun isInAbnormal() = isInAbnormal
    fun isOutAbnormal() = isOutAbnormal


    fun setThresholdIOIn(thresholdIOIn: Double) {
        mThresholdIOIn = thresholdIOIn
    }

    fun getThresholdIOIn() = mThresholdIOIn

    fun setThresholdIOOut(thresholdIOOut: Double) {
        mThresholdIOOut = thresholdIOOut
    }

    fun getThresholdIOOut() = mThresholdIOOut

    /**
     * 异常次数记录
     */
    fun addInAbnormalCnt() {
        mInAbnormalCnt++
    }

    fun getInAbnormalCnt(): Int = mInAbnormalCnt

    fun addOutAbnormalCnt() {
        mOutAbnormalCnt++
    }

    fun getOutAbnormalCnt(): Int = mOutAbnormalCnt

    fun resetInAbnormalCnt() {
        mInAbnormalCnt = 0
    }

    fun resetOutAbnormalCnt() {
        mOutAbnormalCnt = 0
    }
}